1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module soup.Cookie;
26 
27 private import glib.ConstructionException;
28 private import glib.MemorySlice;
29 private import glib.Str;
30 private import glib.c.functions;
31 private import gobject.ObjectG;
32 private import linker.Loader;
33 private import soup.Date;
34 private import soup.URI;
35 private import soup.c.functions;
36 public  import soup.c.types;
37 
38 
39 /**
40  * An HTTP cookie.
41  * 
42  * @name and @value will be set for all cookies. If the cookie is
43  * generated from a string that appears to have no name, then @name
44  * will be the empty string.
45  * 
46  * @domain and @path give the host or domain, and path within that
47  * host/domain, to restrict this cookie to. If @domain starts with
48  * ".", that indicates a domain (which matches the string after the
49  * ".", or any hostname that has @domain as a suffix). Otherwise, it
50  * is a hostname and must match exactly.
51  * 
52  * @expires will be non-%NULL if the cookie uses either the original
53  * "expires" attribute, or the newer "max-age" attribute. If @expires
54  * is %NULL, it indicates that neither "expires" nor "max-age" was
55  * specified, and the cookie expires at the end of the session.
56  * 
57  * If @http_only is set, the cookie should not be exposed to untrusted
58  * code (eg, javascript), so as to minimize the danger posed by
59  * cross-site scripting attacks.
60  *
61  * Since: 2.24
62  */
63 public final class Cookie
64 {
65 	/** the main Gtk struct */
66 	protected SoupCookie* soupCookie;
67 	protected bool ownedRef;
68 
69 	/** Get the main Gtk struct */
70 	public SoupCookie* getCookieStruct(bool transferOwnership = false)
71 	{
72 		if (transferOwnership)
73 			ownedRef = false;
74 		return soupCookie;
75 	}
76 
77 	/** the main Gtk struct as a void* */
78 	protected void* getStruct()
79 	{
80 		return cast(void*)soupCookie;
81 	}
82 
83 	/**
84 	 * Sets our main struct and passes it to the parent class.
85 	 */
86 	public this (SoupCookie* soupCookie, bool ownedRef = false)
87 	{
88 		this.soupCookie = soupCookie;
89 		this.ownedRef = ownedRef;
90 	}
91 
92 	~this ()
93 	{
94 		if ( Linker.isLoaded(LIBRARY_SOUP[0]) && ownedRef )
95 			soup_cookie_free(soupCookie);
96 	}
97 
98 
99 	/**
100 	 * the cookie name
101 	 */
102 	public @property string name()
103 	{
104 		return Str.toString(soupCookie.name);
105 	}
106 
107 	/** Ditto */
108 	public @property void name(string value)
109 	{
110 		soupCookie.name = Str.toStringz(value);
111 	}
112 
113 	/**
114 	 * the cookie value
115 	 */
116 	public @property string value()
117 	{
118 		return Str.toString(soupCookie.value);
119 	}
120 
121 	/** Ditto */
122 	public @property void value(string value)
123 	{
124 		soupCookie.value = Str.toStringz(value);
125 	}
126 
127 	/**
128 	 * the "domain" attribute, or else the hostname that the
129 	 * cookie came from.
130 	 */
131 	public @property string domain()
132 	{
133 		return Str.toString(soupCookie.domain);
134 	}
135 
136 	/** Ditto */
137 	public @property void domain(string value)
138 	{
139 		soupCookie.domain = Str.toStringz(value);
140 	}
141 
142 	/**
143 	 * the "path" attribute, or %NULL
144 	 */
145 	public @property string path()
146 	{
147 		return Str.toString(soupCookie.path);
148 	}
149 
150 	/** Ditto */
151 	public @property void path(string value)
152 	{
153 		soupCookie.path = Str.toStringz(value);
154 	}
155 
156 	/**
157 	 * the cookie expiration time, or %NULL for a session cookie
158 	 */
159 	public @property Date expires()
160 	{
161 		return ObjectG.getDObject!(Date)(soupCookie.expires, false);
162 	}
163 
164 	/** Ditto */
165 	public @property void expires(Date value)
166 	{
167 		soupCookie.expires = value.getDateStruct();
168 	}
169 
170 	/**
171 	 * %TRUE if the cookie should only be tranferred over SSL
172 	 */
173 	public @property bool secure()
174 	{
175 		return soupCookie.secure != 0;
176 	}
177 
178 	/** Ditto */
179 	public @property void secure(bool value)
180 	{
181 		soupCookie.secure = value;
182 	}
183 
184 	/**
185 	 * %TRUE if the cookie should not be exposed to scripts
186 	 */
187 	public @property bool httpOnly()
188 	{
189 		return soupCookie.httpOnly != 0;
190 	}
191 
192 	/** Ditto */
193 	public @property void httpOnly(bool value)
194 	{
195 		soupCookie.httpOnly = value;
196 	}
197 
198 	/** */
199 	public static GType getType()
200 	{
201 		return soup_cookie_get_type();
202 	}
203 
204 	/**
205 	 * Creates a new #SoupCookie with the given attributes. (Use
206 	 * soup_cookie_set_secure() and soup_cookie_set_http_only() if you
207 	 * need to set those attributes on the returned cookie.)
208 	 *
209 	 * If @domain starts with ".", that indicates a domain (which matches
210 	 * the string after the ".", or any hostname that has @domain as a
211 	 * suffix). Otherwise, it is a hostname and must match exactly.
212 	 *
213 	 * @max_age is used to set the "expires" attribute on the cookie; pass
214 	 * -1 to not include the attribute (indicating that the cookie expires
215 	 * with the current session), 0 for an already-expired cookie, or a
216 	 * lifetime in seconds. You can use the constants
217 	 * %SOUP_COOKIE_MAX_AGE_ONE_HOUR, %SOUP_COOKIE_MAX_AGE_ONE_DAY,
218 	 * %SOUP_COOKIE_MAX_AGE_ONE_WEEK and %SOUP_COOKIE_MAX_AGE_ONE_YEAR (or
219 	 * multiples thereof) to calculate this value. (If you really care
220 	 * about setting the exact time that the cookie will expire, use
221 	 * soup_cookie_set_expires().)
222 	 *
223 	 * Params:
224 	 *     name = cookie name
225 	 *     value = cookie value
226 	 *     domain = cookie domain or hostname
227 	 *     path = cookie path, or %NULL
228 	 *     maxAge = max age of the cookie, or -1 for a session cookie
229 	 *
230 	 * Returns: a new #SoupCookie.
231 	 *
232 	 * Since: 2.24
233 	 *
234 	 * Throws: ConstructionException GTK+ fails to create the object.
235 	 */
236 	public this(string name, string value, string domain, string path, int maxAge)
237 	{
238 		auto __p = soup_cookie_new(Str.toStringz(name), Str.toStringz(value), Str.toStringz(domain), Str.toStringz(path), maxAge);
239 
240 		if(__p is null)
241 		{
242 			throw new ConstructionException("null returned by new");
243 		}
244 
245 		this(cast(SoupCookie*) __p);
246 	}
247 
248 	/**
249 	 * Tests if @cookie should be sent to @uri.
250 	 *
251 	 * (At the moment, this does not check that @cookie's domain matches
252 	 * @uri, because it assumes that the caller has already done that.
253 	 * But don't rely on that; it may change in the future.)
254 	 *
255 	 * Params:
256 	 *     uri = a #SoupURI
257 	 *
258 	 * Returns: %TRUE if @cookie should be sent to @uri, %FALSE if
259 	 *     not
260 	 *
261 	 * Since: 2.24
262 	 */
263 	public bool appliesToUri(URI uri)
264 	{
265 		return soup_cookie_applies_to_uri(soupCookie, (uri is null) ? null : uri.getURIStruct()) != 0;
266 	}
267 
268 	/**
269 	 * Copies @cookie.
270 	 *
271 	 * Returns: a copy of @cookie
272 	 *
273 	 * Since: 2.24
274 	 */
275 	public Cookie copy()
276 	{
277 		auto __p = soup_cookie_copy(soupCookie);
278 
279 		if(__p is null)
280 		{
281 			return null;
282 		}
283 
284 		return ObjectG.getDObject!(Cookie)(cast(SoupCookie*) __p, true);
285 	}
286 
287 	/**
288 	 * Checks if the @cookie's domain and @host match in the sense that
289 	 * @cookie should be sent when making a request to @host, or that
290 	 * @cookie should be accepted when receiving a response from @host.
291 	 *
292 	 * Params:
293 	 *     host = a URI
294 	 *
295 	 * Returns: %TRUE if the domains match, %FALSE otherwise
296 	 *
297 	 * Since: 2.30
298 	 */
299 	public bool domainMatches(string host)
300 	{
301 		return soup_cookie_domain_matches(soupCookie, Str.toStringz(host)) != 0;
302 	}
303 
304 	/**
305 	 * Tests if @cookie1 and @cookie2 are equal.
306 	 *
307 	 * Note that currently, this does not check that the cookie domains
308 	 * match. This may change in the future.
309 	 *
310 	 * Params:
311 	 *     cookie2 = a #SoupCookie
312 	 *
313 	 * Returns: whether the cookies are equal.
314 	 *
315 	 * Since: 2.24
316 	 */
317 	public bool equal(Cookie cookie2)
318 	{
319 		return soup_cookie_equal(soupCookie, (cookie2 is null) ? null : cookie2.getCookieStruct()) != 0;
320 	}
321 
322 	/**
323 	 * Frees @cookie
324 	 *
325 	 * Since: 2.24
326 	 */
327 	public void free()
328 	{
329 		soup_cookie_free(soupCookie);
330 		ownedRef = false;
331 	}
332 
333 	/**
334 	 * Gets @cookie's domain
335 	 *
336 	 * Returns: @cookie's domain
337 	 *
338 	 * Since: 2.32
339 	 */
340 	public string getDomain()
341 	{
342 		return Str.toString(soup_cookie_get_domain(soupCookie));
343 	}
344 
345 	/**
346 	 * Gets @cookie's expiration time.
347 	 *
348 	 * Returns: @cookie's expiration
349 	 *     time, which is owned by @cookie and should not be modified or
350 	 *     freed.
351 	 *
352 	 * Since: 2.32
353 	 */
354 	public Date getExpires()
355 	{
356 		auto __p = soup_cookie_get_expires(soupCookie);
357 
358 		if(__p is null)
359 		{
360 			return null;
361 		}
362 
363 		return ObjectG.getDObject!(Date)(cast(SoupDate*) __p);
364 	}
365 
366 	/**
367 	 * Gets @cookie's HttpOnly attribute
368 	 *
369 	 * Returns: @cookie's HttpOnly attribute
370 	 *
371 	 * Since: 2.32
372 	 */
373 	public bool getHttpOnly()
374 	{
375 		return soup_cookie_get_http_only(soupCookie) != 0;
376 	}
377 
378 	/**
379 	 * Gets @cookie's name
380 	 *
381 	 * Returns: @cookie's name
382 	 *
383 	 * Since: 2.32
384 	 */
385 	public string getName()
386 	{
387 		return Str.toString(soup_cookie_get_name(soupCookie));
388 	}
389 
390 	/**
391 	 * Gets @cookie's path
392 	 *
393 	 * Returns: @cookie's path
394 	 *
395 	 * Since: 2.32
396 	 */
397 	public string getPath()
398 	{
399 		return Str.toString(soup_cookie_get_path(soupCookie));
400 	}
401 
402 	/**
403 	 * Returns: a #SoupSameSitePolicy
404 	 *
405 	 * Since: 2.70
406 	 */
407 	public SoupSameSitePolicy getSameSitePolicy()
408 	{
409 		return soup_cookie_get_same_site_policy(soupCookie);
410 	}
411 
412 	/**
413 	 * Gets @cookie's secure attribute
414 	 *
415 	 * Returns: @cookie's secure attribute
416 	 *
417 	 * Since: 2.32
418 	 */
419 	public bool getSecure()
420 	{
421 		return soup_cookie_get_secure(soupCookie) != 0;
422 	}
423 
424 	/**
425 	 * Gets @cookie's value
426 	 *
427 	 * Returns: @cookie's value
428 	 *
429 	 * Since: 2.32
430 	 */
431 	public string getValue()
432 	{
433 		return Str.toString(soup_cookie_get_value(soupCookie));
434 	}
435 
436 	/**
437 	 * Sets @cookie's domain to @domain
438 	 *
439 	 * Params:
440 	 *     domain = the new domain
441 	 *
442 	 * Since: 2.24
443 	 */
444 	public void setDomain(string domain)
445 	{
446 		soup_cookie_set_domain(soupCookie, Str.toStringz(domain));
447 	}
448 
449 	/**
450 	 * Sets @cookie's expiration time to @expires. If @expires is %NULL,
451 	 * @cookie will be a session cookie and will expire at the end of the
452 	 * client's session.
453 	 *
454 	 * (This sets the same property as soup_cookie_set_max_age().)
455 	 *
456 	 * Params:
457 	 *     expires = the new expiration time, or %NULL
458 	 *
459 	 * Since: 2.24
460 	 */
461 	public void setExpires(Date expires)
462 	{
463 		soup_cookie_set_expires(soupCookie, (expires is null) ? null : expires.getDateStruct());
464 	}
465 
466 	/**
467 	 * Sets @cookie's HttpOnly attribute to @http_only. If %TRUE, @cookie
468 	 * will be marked as "http only", meaning it should not be exposed to
469 	 * web page scripts or other untrusted code.
470 	 *
471 	 * Params:
472 	 *     httpOnly = the new value for the HttpOnly attribute
473 	 *
474 	 * Since: 2.24
475 	 */
476 	public void setHttpOnly(bool httpOnly)
477 	{
478 		soup_cookie_set_http_only(soupCookie, httpOnly);
479 	}
480 
481 	/**
482 	 * Sets @cookie's max age to @max_age. If @max_age is -1, the cookie
483 	 * is a session cookie, and will expire at the end of the client's
484 	 * session. Otherwise, it is the number of seconds until the cookie
485 	 * expires. You can use the constants %SOUP_COOKIE_MAX_AGE_ONE_HOUR,
486 	 * %SOUP_COOKIE_MAX_AGE_ONE_DAY, %SOUP_COOKIE_MAX_AGE_ONE_WEEK and
487 	 * %SOUP_COOKIE_MAX_AGE_ONE_YEAR (or multiples thereof) to calculate
488 	 * this value. (A value of 0 indicates that the cookie should be
489 	 * considered already-expired.)
490 	 *
491 	 * (This sets the same property as soup_cookie_set_expires().)
492 	 *
493 	 * Params:
494 	 *     maxAge = the new max age
495 	 *
496 	 * Since: 2.24
497 	 */
498 	public void setMaxAge(int maxAge)
499 	{
500 		soup_cookie_set_max_age(soupCookie, maxAge);
501 	}
502 
503 	/**
504 	 * Sets @cookie's name to @name
505 	 *
506 	 * Params:
507 	 *     name = the new name
508 	 *
509 	 * Since: 2.24
510 	 */
511 	public void setName(string name)
512 	{
513 		soup_cookie_set_name(soupCookie, Str.toStringz(name));
514 	}
515 
516 	/**
517 	 * Sets @cookie's path to @path
518 	 *
519 	 * Params:
520 	 *     path = the new path
521 	 *
522 	 * Since: 2.24
523 	 */
524 	public void setPath(string path)
525 	{
526 		soup_cookie_set_path(soupCookie, Str.toStringz(path));
527 	}
528 
529 	/**
530 	 * When used in conjunction with soup_cookie_jar_get_cookie_list_with_same_site_info() this
531 	 * sets the policy of when this cookie should be exposed.
532 	 *
533 	 * Params:
534 	 *     policy = a #SoupSameSitePolicy
535 	 *
536 	 * Since: 2.70
537 	 */
538 	public void setSameSitePolicy(SoupSameSitePolicy policy)
539 	{
540 		soup_cookie_set_same_site_policy(soupCookie, policy);
541 	}
542 
543 	/**
544 	 * Sets @cookie's secure attribute to @secure. If %TRUE, @cookie will
545 	 * only be transmitted from the client to the server over secure
546 	 * (https) connections.
547 	 *
548 	 * Params:
549 	 *     secure = the new value for the secure attribute
550 	 *
551 	 * Since: 2.24
552 	 */
553 	public void setSecure(bool secure)
554 	{
555 		soup_cookie_set_secure(soupCookie, secure);
556 	}
557 
558 	/**
559 	 * Sets @cookie's value to @value
560 	 *
561 	 * Params:
562 	 *     value = the new value
563 	 *
564 	 * Since: 2.24
565 	 */
566 	public void setValue(string value)
567 	{
568 		soup_cookie_set_value(soupCookie, Str.toStringz(value));
569 	}
570 
571 	/**
572 	 * Serializes @cookie in the format used by the Cookie header (ie, for
573 	 * returning a cookie from a #SoupSession to a server).
574 	 *
575 	 * Returns: the header
576 	 *
577 	 * Since: 2.24
578 	 */
579 	public string toCookieHeader()
580 	{
581 		auto retStr = soup_cookie_to_cookie_header(soupCookie);
582 
583 		scope(exit) Str.freeString(retStr);
584 		return Str.toString(retStr);
585 	}
586 
587 	/**
588 	 * Serializes @cookie in the format used by the Set-Cookie header
589 	 * (ie, for sending a cookie from a #SoupServer to a client).
590 	 *
591 	 * Returns: the header
592 	 *
593 	 * Since: 2.24
594 	 */
595 	public string toSetCookieHeader()
596 	{
597 		auto retStr = soup_cookie_to_set_cookie_header(soupCookie);
598 
599 		scope(exit) Str.freeString(retStr);
600 		return Str.toString(retStr);
601 	}
602 
603 	/**
604 	 * Parses @header and returns a #SoupCookie. (If @header contains
605 	 * multiple cookies, only the first one will be parsed.)
606 	 *
607 	 * If @header does not have "path" or "domain" attributes, they will
608 	 * be defaulted from @origin. If @origin is %NULL, path will default
609 	 * to "/", but domain will be left as %NULL. Note that this is not a
610 	 * valid state for a #SoupCookie, and you will need to fill in some
611 	 * appropriate string for the domain if you want to actually make use
612 	 * of the cookie.
613 	 *
614 	 * Params:
615 	 *     header = a cookie string (eg, the value of a Set-Cookie header)
616 	 *     origin = origin of the cookie, or %NULL
617 	 *
618 	 * Returns: a new #SoupCookie, or %NULL if it could
619 	 *     not be parsed, or contained an illegal "domain" attribute for a
620 	 *     cookie originating from @origin.
621 	 *
622 	 * Since: 2.24
623 	 */
624 	public static Cookie parse(string header, URI origin)
625 	{
626 		auto __p = soup_cookie_parse(Str.toStringz(header), (origin is null) ? null : origin.getURIStruct());
627 
628 		if(__p is null)
629 		{
630 			return null;
631 		}
632 
633 		return ObjectG.getDObject!(Cookie)(cast(SoupCookie*) __p, true);
634 	}
635 }